package cn.sirun.com.fca.account.observer;

import android.support.annotation.CallSuper;

import com.tencent.cloud.iov.flow.presenter.IActionPresenter;
import com.tencent.cloud.iov.flow.presenter.impl.BaseFlowPresenter;
import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.util.rx.SimpleObserver;

import cn.sirun.com.fca.account.constant.AccountActionConst;

/**
 * 通用异常观察者，将本地异常信息统一处理
 * <p>
 * 注：使用该观察者，FlowView需增加AccountActionConst.Normal.ACTION_REQUEST_EXCEPTION的回调处理，
 * 或者FlowView继承AccountBaseLoadingFlowView
 *
 * @author vinsonhuang
 * @date 2018/8/24
 */
public class CommonExceptionObserver<T> extends SimpleObserver<BaseResponse<T>> {

    private IActionPresenter mActionPresenter;

    private BaseFlowPresenter mBaseFlowPresenter;

    public CommonExceptionObserver(IActionPresenter actionPresenter) {
        mActionPresenter = actionPresenter;
    }

    public CommonExceptionObserver(BaseFlowPresenter baseFlowPresenter) {
        mBaseFlowPresenter = baseFlowPresenter;
    }

    @CallSuper
    @Override
    public void onError(Throwable throwable) {
        super.onError(throwable);
        if (null != mActionPresenter) {
            mActionPresenter.onAction(AccountActionConst.Normal.ACTION_REQUEST_EXCEPTION,
                    throwable);
            return;
        }
        if (null != mBaseFlowPresenter) {
            mBaseFlowPresenter.onAction(AccountActionConst.Normal.ACTION_REQUEST_EXCEPTION,
                    throwable);
        }

    }
}
